<template>
  <div>
    <el-card shadow="never">
      <div slot="header" class="clearfix">
        <span>搜索</span>
      </div>
      <div class="text item">
        <xdh-form :model="model" :fields="fields" @submit="handleSubmit">
        </xdh-form>
      </div>
    </el-card>

    <el-card shadow="never" style="margin-top:20px;">
      <div slot="header" class="clearfix">
        <span>列表</span>
      </div>
      <div class="text item">

        <div class="cont">
          <el-table ref="dragTable" id="crTable" row-key="id" :border='true' :data="formData.domains" class="tb-edit" style="width: 100%" highlight-current-row>
            <template v-for="(item,index) in tablefields">
              <el-table-column align="center" :label="item.label" :key="item.prop" v-bind="item">
                <template slot="header">
                  <div class="headeritem">
                    <span>{{item.label}}</span>
                  </div>
                </template>
                <template slot-scope="{row}">
                  <xdh-form ref="form" class="design-form" :footer="false" :model="tableModel" design-mode>
                    <xdh-form-item v-bind="clone(item,row)" label="" :labelWidth="'0px'" :type="item.type" :prop="item.prop" :value="row[item.prop]">
                    </xdh-form-item>
                  </xdh-form>
                </template>
              </el-table-column>
            </template>
          </el-table>

        </div>
      </div>
    </el-card>

  </div>
</template>
<style lang="stylus" rel="stylesheet/stylus" scoped type="text/stylus">
.node {
  position: relative;
  font-size: 16px;
  padding: 5px;
  padding-top: 10px;

  &::before {
    content: '';
    display: block;
    overflow: hidden;
    height: 0;
    width: 1px;
  }
}
</style>
<script type="text/ecmascript-6">
import XdhForm from '@/widgets/xdh-form'
import XdhFormItem from '@/widgets/xdh-form/xdh-form-item'
import FormMixin from '@/base/mixin/forms'
import cloneDeep from 'lodash/cloneDeep'
import Vue from 'vue'
import { getForms } from '@/widgets/helper/storage'

export default {
  name: 'tablePrenode',
  components: {
    XdhForm, XdhFormItem
  },
  mixins: [FormMixin],
  props: {
    scope: {
      type: Object
    },
    stacked: {
      type: Boolean,
      default: true
    },
    info: {
      required: true,
      type: Array
    },
    tableinfo: {
      required: true,
      type: Array
    },
    slotStyle: {
      type: Object,
      default: null
    }
  },
  data: function () {
    return {
      oldId: '',
      tab_id: '0',
      isActive: false,
      currPage: '',
      oldStyle: {},
      nodeInfo: (() => {
        if (this.scope) {
          return cloneDeep(this.info)
        } else {
          console.log(this.info, 'info================================')
          return this.info[0]['content']
        }
      })(),
      fields: [],
      tablefields: [],
      load: null,
      formData: {
        domains: [{ id: 1, title: "你好中国人" }, { id: 2, title: "你好美国人" }]
      },
    }
  },
  computed: {
    model() {
      const m = {}
      this.fields.forEach(item => {
        if (!item) return
        // 分隔线组件无值
        if (item.type !== 'divider') {
          m[item.prop] = (item.props || {}).value || item.value
        }
      })
      return m
    },
    tableModel() {
      const m = {}
      this.tablefields.forEach(item => {
        if (!item) return
        // 分隔线组件无值
        if (item.type !== 'divider') {
          m[item.prop] = (item.props || {}).value || item.value
        }
      })
      return m
    }
  },
  watch: {
    info: {
      deep: true,
      handler: function (newVal) {
        console.log(newVal, 'info================================')
        var info = {}
        if (this.scope) {
          info = cloneDeep(newVal)
        } else {
          info = newVal
        }
        this.nodeInfo = info
      }
    },
  },
  created() {
    this.fields = this.nodeInfo || []
    this.tablefields = this.tableinfo || []
  },
  mounted() {

  },
  methods: {
    clone(data, row) {
      var result = this._.cloneDeep(data);
      result.props['value'] = row[data.prop];
      console.log('clone', data, row[data.prop], result)
      return result
    },
    handleSubmit(model) {
      this.$message({
        message: '若要保存页面，请点击头部的保存按钮',
        type: 'success'
      });
      console.log('submit', model, this.fields)
    },
    cloneDeep: cloneDeep,
  }
}
</script>

